import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/page/common/common_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/train/model/train_model.dart';
import 'package:flutter_learn_vi/src/utils/date_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/27.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class ListItemTrainRecoderItem3 extends StatefulWidget {
  final int index;
  final List<TrainCourseSignBean> list;

  ///Ctrl + 回车键自动提示补全
  const ListItemTrainRecoderItem3({Key key, this.index, this.list})
      : super(key: key);

  @override
  _ListItemTrainRecoderItem3State createState() =>
      _ListItemTrainRecoderItem3State();
}

class _ListItemTrainRecoderItem3State extends State<ListItemTrainRecoderItem3>
    with TrainModel {
  int _index;
  List<TrainCourseSignBean> _list;
  TrainCourseSignBean _signRecoderBean;
  TrainCourseSignBean _preSignRecoderBean;

  @override
  void initState() {
    super.initState();
    _index = widget.index;
    _list = widget.list;
    _signRecoderBean = _list[_index];
    if (_index > 0) {
      _preSignRecoderBean = _list[_index - 1];
    }
  }

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Container(
        color: Colors.white,
        //左右排列
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //左侧的标签
            buildLeftFlagWidget(),
            //右侧的内容
            Expanded(
              child: buildRightWidget(),
            ),
          ],
        ),
      ),
    );
  }

  buildLeftFlagWidget() {
    String signTime = ViDateUtils.formatDateStr(
      _signRecoderBean.createTime,
    );
    String preSignTime = "";
    if (_preSignRecoderBean != null) {
      preSignTime = ViDateUtils.formatDateStr(
        _preSignRecoderBean.createTime,
      );
    }
    double cirWidth = 10;
    if (signTime == preSignTime) {
      cirWidth = 0;
    }
    return Container(
      margin: EdgeInsets.only(left: 12, right: 10),
      child: Column(
        children: [
          Container(
            width: 1,
            color: widget.index == 0 ? Colors.transparent : Colors.blueGrey,
            height: 12,
          ),
          Container(
            width: 10,
            height: cirWidth,
            //子Widget居中
            alignment: Alignment.center,
            //样式
            decoration: BoxDecoration(
                color: Colors.green,
                //四个角的圆角
                borderRadius: BorderRadius.all(Radius.circular(10))),
          ),
          Expanded(
              child: Container(
            width: 1,
            color: Colors.blueGrey,
          ))
        ],
      ),
    );
  }

  buildRightWidget() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      padding: EdgeInsets.only(right: 16, bottom: 20),
      height: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(left: 8),
            child: buildColumn(),
          )
        ],
      ),
    );
  }

  Column buildColumn() {
    //1 面授课程  2 在线课程
    int signType = 1;
    if (signType == 1) {
      return buildCommonSignColumn();
    } else {
      return buildVideoStudyColumn();
    }
  }

  ///常规普通打卡签到
  Column buildCommonSignColumn() {
    return Column(
      children: [
        Row(
          children: [
            Text(
              "时间:",
              style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.w600,
                  fontSize: 14),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "${_signRecoderBean.createTime}",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.w600,
                    fontSize: 14),
              ),
            )
          ],
        ),
        SizedBox(height: 8),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "课程:",
              style: TextStyle(color: Colors.blueGrey),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "${_signRecoderBean.lineCourseName}",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                ),
              ),
            )
          ],
        ),
        buildRow(),
      ],
    );
  }

  Widget buildRow() {
    String signUpTime = _signRecoderBean.signUpTime;
    String signToTime = _signRecoderBean.signToTime;
    if (_isLoading) {
      return LoadingWidget();
    }

    if (signToTime == null || signToTime.isEmpty) {
      return SizedBox();
    }
    if (signUpTime == null || signUpTime.isEmpty) {
      signUpTime = "暂无";
    }

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          children: [
            Text(
              "签到时间:",
              style: TextStyle(color: Colors.blueGrey),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "$signToTime",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.blueGrey,
                ),
              ),
            )
          ],
        ),
        Row(
          children: [
            Text(
              "签退时间:",
              style: TextStyle(color: Colors.blueGrey),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "$signUpTime",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.blueGrey,
                ),
              ),
            )
          ],
        ),
      ],
    );
  }

  void signUpCheckFunction() {
    showCommonAlertDialog(
        context: context,
        cancleText: "取消",
        selectText: "确定",
        contentMessag: "确定签退吗??",
        selectCallBack: () {});
  }

  bool _isLoading = false;

  ///签退

  Column buildVideoStudyColumn() {
    String signTime = ViDateUtils.formatDateStr(
      _signRecoderBean.createTime,
    );
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          children: [
            Text(
              "在线课程:",
              style: TextStyle(color: Colors.blueGrey),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "${_signRecoderBean.createTime}",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.blueGrey,
                ),
              ),
            )
          ],
        ),
        Row(
          children: [
            Text(
              "参与时间:",
              style: TextStyle(color: Colors.blueGrey),
            ),
            SizedBox(
              width: 8,
            ),
            Expanded(
              child: Text(
                "${_signRecoderBean.createTime}",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.blueGrey,
                ),
              ),
            )
          ],
        )
      ],
    );
  }
}
